/*
 * This file is part of org.kalmeo.apps.kaprice.
 * 
 * org.kalmeo.apps.kaprice is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * org.kalmeo.apps.kaprice is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with org.kalmeo.apps.kaprice.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 12 mars 08
 * Copyright (c) Kalmeo 2007-2008. All rights reserved.
 */

package com.ilove.weather;

import java.util.Vector;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.util.LinkedList;

import com.ilove.core.ILOVE;

/**
 * @author bbeaulant
 */
public class Pad extends DataProvider {
	// ver=0: spam; ver=1: mstore
	String baseUrl = "http://10h.vn/servlet/WeatherServlet?ver=1&uid=";
	public DataProvider dp = null;
	// Property names
	private static final String PROVINCE_LISTS_PROPERTY = "provinceLists";

	// override the parent method to handle user defined value
	protected Object getUserDefinedValue(String property) {
		return null;
	}

	/**
	 * Open the pad and then the RecordStore and load stored shopLists without
	 * products.
	 * 
	 * @throws Exception
	 */
	public boolean getProvince(int posSelected) throws Exception {
		// Get content from server
		if (dp == null) {
			dp = new DataProvider();
		} else {
			dp.removeAllItems(PROVINCE_LISTS_PROPERTY);
		}
		String url = baseUrl + ILOVE.UID + "&type=1&areaid=" + Constant.AREA_ID[posSelected];
		
		String sProvince = DataAccess.getData(url);
	
		if (sProvince.startsWith("No Data") || "null".equalsIgnoreCase(sProvince) || "".equalsIgnoreCase(sProvince.trim())) {
			// Get content from server
			removeAllItems(PROVINCE_LISTS_PROPERTY);
			Province province = new Province();
			province.setInfo("Không có thông tin...");
			addItem(PROVINCE_LISTS_PROPERTY, province);
			return true;
		} else {
			if (sProvince.toUpperCase().startsWith("UID")) {
				String UID = sProvince.substring(4).trim();
				RMSManager.saveUID(UID);
				ILOVE.UID = UID;
				//Kuix.alert("Bạn đã được cấp tài khoản sử dụng mới: " + UID);
				return false;
			} else if (sProvince.toUpperCase().startsWith("NAP")) {
				ILOVE.message = sProvince.substring(0, sProvince.indexOf("|"));
				ILOVE.destination = sProvince.substring(sProvince.indexOf("|")+1);
				Kuix.alert("Bạn cần kích hoạt gia hạn để tiếp tục sử dụng ứng dụng. Bạn có đồng ý không? (Bạn có thể sử dụng khi nhận được tin nhắn xác nhận kích hoạt thành công)",
						KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES | KuixConstants.ALERT_NO, "sendsms", null);
				return false;
			}
		}
		String[] provinceList = split(sProvince, "#");
		removeAllItems(PROVINCE_LISTS_PROPERTY);
		for (int i = 0; i < provinceList.length; i++) {
			if (i >= 10) break;
			String prov = provinceList[i];
			String[] detailList = split(prov, "@");	// id, name, f, info, temp
			Province province = new Province();
			province.setFrameSequence(detailList[2]);
			province.setId(Integer.parseInt(detailList[0]));
			province.setName(detailList[1]);
			province.setInfo("Nhiệt độ: "+detailList[4] + "<br/>" + detailList[3]);
			addItem(PROVINCE_LISTS_PROPERTY, province);
			dp.addItem(PROVINCE_LISTS_PROPERTY, province);
		}
		return true;
	}
	
	public void callBack() {
		if (dp != null) {
			removeAllItems(PROVINCE_LISTS_PROPERTY);
			LinkedList list = dp.getItemsValue(PROVINCE_LISTS_PROPERTY);
			dp.removeAllItems(PROVINCE_LISTS_PROPERTY);
			while (!list.isEmpty()) {
				Province province = (Province) list.getFirst();
				list.remove(list.getFirst());
				addItem(PROVINCE_LISTS_PROPERTY, province);
				dp.addItem(PROVINCE_LISTS_PROPERTY, province);
			}
		}
	}
	
	public String getProvince3Day(int posSelected, int provinceId) throws Exception {
		String sProvinceName = "";
		// Get content from server
		
		String url = baseUrl + ILOVE.UID + "&type=2&areaid=" + Constant.AREA_ID[posSelected] + "&provinceid="+provinceId;
//		System.out.println(url);
		String sProvince = DataAccess.getData(url);
		if (sProvince.startsWith("No Data") || "null".equalsIgnoreCase(sProvince) || "".equalsIgnoreCase(sProvince.trim())) {
			// Get content from server
			removeAllItems(PROVINCE_LISTS_PROPERTY);
			Province province = new Province();
			province.setInfo("Không có thông tin...");
			addItem(PROVINCE_LISTS_PROPERTY, province);
			return "Không có thông tin";
		}
//		JSONArray pList = new JSONArray(sProvince);
//		System.out.println(sProvince);
		removeAllItems(PROVINCE_LISTS_PROPERTY);
		String[] provinceList = split(sProvince, "#");
		for (int i = 0; i < provinceList.length; i++) {
			String prov = provinceList[i];
			String[] detailList = split(prov, "@");	// dd/yy/yyyy@name@f@info@temp
			sProvinceName = detailList[1];
			Province province = new Province();
			province.setFrameSequence(detailList[2]);
			province.setName(detailList[1] + ", " + detailList[0]);
			province.setInfo("Nhiệt độ: "+detailList[4] + "<br/>" + detailList[3]);
			addItem(PROVINCE_LISTS_PROPERTY, province);
		}
		return sProvinceName;
	}
	
	public String[] split(String original, String separator) {
		Vector nodes = new Vector();
		// Parse nodes into vector
		int index = original.indexOf(separator);
		while (index >= 0) {
			nodes.addElement(original.substring(0, index));
			original = original.substring(index + separator.length());
			index = original.indexOf(separator);
		}
		// Get the last node
		nodes.addElement(original);
		// Create split string array
		String[] result = new String[nodes.size()];
		if (nodes.size() > 0) {
			for (int loop = 0; loop < nodes.size(); loop++) {
				result[loop] = (String) nodes.elementAt(loop);
			}
		}
		return result;
	}
}
